package controller

import (
	"express/internal/model"
	"express/internal/service"
	"express/pkg/app"
	"github.com/gin-gonic/gin"
)

type ParcelController struct {
}

func NewParcelController() *ParcelController {
	return &ParcelController{}
}

func (p *ParcelController) SendParcel(c *gin.Context) {
	var param model.SendParcelParam
	c.ShouldBind(&param)

	parcelService := service.NewParcelService()
	err := parcelService.CreatRecord(&param)
	if err != nil {
		c.JSON(500, gin.H{"msg": "server error"})
		return
	}
	app.Success(c, "")
}

func (p *ParcelController) GetParcelListByReceiver(c *gin.Context) {
	phone := c.Query("phone")

	parcelService := service.NewParcelService()
	list, err := parcelService.GetParcelByPhone(phone)
	if err != nil {
		c.JSON(500, "error")
	}
	app.Success(c, list)
}

func (p *ParcelController) GetParcelListBySender(c *gin.Context) {
	phone := c.Query("phone")
	parcelService := service.NewParcelService()
	list, err := parcelService.GetParcelBySender(phone)
	if err != nil {
		c.JSON(500, "error")
	}
	app.Success(c, list)
}
